//
//  ViewController.m
//  相片的选择
//
//  Created by xiaomage on 16/3/27.
//  Copyright © 2016年 小码哥. All rights reserved.
//
/*
 一、从相册里面选择图片到App中
 1.选择单张图片
 1> UIImagePickerController (自带选择界面)
 2> AssetsLibrary框架 (选择界面需要开发者自己搭建)
 3> Photos框架 (选择界面需要开发者自己搭建)
 
 2.选择多张图片(图片数量 >= 2)
 1> AssetsLibrary框架 (选择界面需要开发者自己搭建)
 2> Photos框架 (选择界面需要开发者自己搭建)
 
 二、利用照相机拍一张照片到App
 1> UIImagePickerController (自带选择界面)
 2> AVCapture****，比如AVCaptureSeession
 */

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}

- (IBAction)selectPhoto {
    // UIAlertControllerStyleAlert : 相当于弹出一个UIAlertView
    // UIAlertControllerStyleActionSheet : 相当于弹出一个UIActionSheet
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    // 按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openAlbum];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

/**
 *  打开相册
 */
- (void)openAlbum
{
    // UIImagePickerControllerSourceTypeSavedPhotosAlbum : 从Moments相册中选一张图片
    // UIImagePickerControllerSourceTypePhotoLibrary : 从所有相册中选一张图片
    // UIImagePickerControllerSourceTypeCamera : 利用照相机中拍一张图片
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

/**
 *  打开照相机
 */
- (void)openCamera
{
    //判断是否能打开相机
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - <UIImagePickerControllerDelegate>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // 关闭图片选择界面
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 显示选择的图片
    self.imageView.image = info[UIImagePickerControllerOriginalImage];
}
@end
